package com.cxy.demospring;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5EncryptionTool extends JFrame {
    private JTextArea plainTextArea, knownHashArea, resultArea;
    private JButton generateButton, verifyButton;

    public MD5EncryptionTool() {
        setTitle("MD5哈希工具");
        setSize(700, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setLocationRelativeTo(null);

        // 创建文本区域
        plainTextArea = new JTextArea(6, 30);
        knownHashArea = new JTextArea(6, 30);
        resultArea = new JTextArea(6, 30);
        resultArea.setEditable(false);

        // 设置字体以确保中文正常显示
        Font monoFont = new Font("Monospaced", Font.PLAIN, 12);
        plainTextArea.setFont(monoFont);
        knownHashArea.setFont(monoFont);
        resultArea.setFont(monoFont);

        // 创建滚动面板
        JScrollPane plainScrollPane = new JScrollPane(plainTextArea);
        JScrollPane knownHashScrollPane = new JScrollPane(knownHashArea);
        JScrollPane resultScrollPane = new JScrollPane(resultArea);

        // 创建按钮
        generateButton = new JButton("生成MD5");
        verifyButton = new JButton("验证MD5");

        // 设置按钮字体
        Font buttonFont = new Font("SimHei", Font.PLAIN, 14);
        generateButton.setFont(buttonFont);
        verifyButton.setFont(buttonFont);

        // 设置按钮监听器
        generateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                generateMD5();
            }
        });

        verifyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                verifyMD5();
            }
        });

        // 创建标签
        JLabel plainLabel = new JLabel("原始文本:");
        plainLabel.setFont(new Font("SimHei", Font.PLAIN, 14));

        JLabel knownHashLabel = new JLabel("已知MD5值:");
        knownHashLabel.setFont(new Font("SimHei", Font.PLAIN, 14));

        JLabel resultLabel = new JLabel("验证结果:");
        resultLabel.setFont(new Font("SimHei", Font.PLAIN, 14));

        // 创建输入面板
        JPanel inputPanel = new JPanel(new GridLayout(1, 2, 10, 10));
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JPanel leftInputPanel = new JPanel(new BorderLayout(5, 5));
        leftInputPanel.add(plainLabel, BorderLayout.NORTH);
        leftInputPanel.add(plainScrollPane, BorderLayout.CENTER);

        JPanel rightInputPanel = new JPanel(new BorderLayout(5, 5));
        rightInputPanel.add(knownHashLabel, BorderLayout.NORTH);
        rightInputPanel.add(knownHashScrollPane, BorderLayout.CENTER);

        inputPanel.add(leftInputPanel);
        inputPanel.add(rightInputPanel);

        // 创建结果面板
        JPanel resultPanel = new JPanel(new BorderLayout(5, 5));
        resultPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        resultPanel.add(resultLabel, BorderLayout.NORTH);
        resultPanel.add(resultScrollPane, BorderLayout.CENTER);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(generateButton);
        buttonPanel.add(verifyButton);

        // 添加组件到窗口
        add(inputPanel, BorderLayout.NORTH);
        add(resultPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    // 生成MD5值
    private void generateMD5() {
        String plainText = plainTextArea.getText();
        if (plainText.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入要生成MD5值的文本！", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        try {
            String md5Hash = calculateMD5(plainText);
            resultArea.setText("生成的MD5值:\n" + md5Hash);
            knownHashArea.setText(md5Hash); // 自动填充到已知哈希区域
        } catch (Exception ex) {
            resultArea.setText("错误: " + ex.getMessage());
        }
    }

    // 验证MD5值
    private void verifyMD5() {
        String plainText = plainTextArea.getText();
        String knownHash = knownHashArea.getText().trim().toLowerCase();

        if (plainText.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入原始文本！", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        if (knownHash.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入已知MD5值！", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        try {
            String calculatedHash = calculateMD5(plainText);

            if (calculatedHash.equals(knownHash)) {
                resultArea.setText("验证成功！\n输入文本生成的MD5值与已知值匹配。");
            } else {
                resultArea.setText("验证失败！\n输入文本生成的MD5值:\n" + calculatedHash + "\n已知MD5值:\n" + knownHash);
            }
        } catch (Exception ex) {
            resultArea.setText("错误: " + ex.getMessage());
        }
    }

    // 计算MD5值
    private String calculateMD5(String input) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] hashBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));

        // 将字节数组转换为十六进制字符串
        StringBuilder hexString = new StringBuilder();
        for (byte b : hashBytes) {
            String hex = String.format("%02x", b);
            hexString.append(hex);
        }

        return hexString.toString();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MD5EncryptionTool app = new MD5EncryptionTool();
                app.setVisible(true);
            }
        });
    }
}